/*****************************************************************************
	SNEGA UTILITIES 0.7a
	Copyright (C) 2009, Xiaohan Tian (snega0223)
	
	http://code.google.com/p/snega-utilities/
	
	This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License(LGPL) as
	published by the Free Software Foundation, either version 3 of the 
	License, or(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Lesser General Public License for more details.
	
	You should have received a copy of the GNU Lesser General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*****************************************************************************/

package org.sng.util.db.elmt;

import java.util.*;

import org.sng.util.db.*;
import org.sng.util.db.rawsql.*;

public class DataTableIdxString extends DataTable {
	
private HashMap<Integer, DataRow> m_row_map;
	
	private String m_pk;
	
	public DataTableIdxString()
	{
		super();
		
		this.m_row_map = new HashMap<Integer, DataRow>();
		this.m_pk = null;
	}
	
	public DataTableIdxString(SQLSelect sql)
	{
		super(sql);
		
		this.m_row_map = new HashMap<Integer, DataRow>();
		this.m_pk = null;
	}
	
	public DataRow Get(Object index)
	{
		return this.m_row_map.get(index);
	}
	
	public void Refresh()
	{
		this.refresh();
		
		this.m_row_map.clear();
		
		// get PK
		DbConn pk = this.m_dbo.getPrimaryKeys(this.m_sql.GetTarget().ToString());
		
		try {
			pk.rs.next();
			this.m_pk = pk.rs.getString(4);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		pk.close();
		
		// build hash map
		for (int i = 0; i < this.m_rows.length; i++)
		{
			this.m_row_map.put(this.m_rows[i].GetCell(this.m_pk).GetInt(), this.m_rows[i]);
		}
	}

}
